<?php

namespace Admin\Controller;

use Think\Controller;

abstract class AdminController extends Controller {

    protected $admin_info;
    protected $setting;
    protected $http;
    protected $page_size;

    public function __construct() {
        parent::__construct();
        header('Content-Type:text/html;charset=utf-8');
        $this->checkLogin();
        $this->checkAuth();
        $this->setting = C('setting');
        $this->initSetting();
        $this->http = $_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http';
        $this->page_size = (int)C('PAGE_SIZE') > 0 ? (int)C('PAGE_SIZE') : 20;
    }

    protected function initSetting() {

    }

    public function success($message = '', $jumpUrl = '', $session = true) {
        if (is_string($message) && $session) {
            session('__SUCCESS_MESSAGE', $message);
        }
        if (IS_AJAX) {
            ajax_return(1, $message, ['url' => $jumpUrl]);
        } else {
            if (!$jumpUrl) {
                $jumpUrl = $_SERVER['HTTP_REFERER'];
            }
            parent::redirect($jumpUrl);
            exit;
        }
    }

    public function error($message = '', $jumpUrl = '', $session = true) {

        if (IS_AJAX) {
            ajax_return(0, $message, ['url' => $jumpUrl]);
        } else {
            if (!$jumpUrl) {
                $jumpUrl = $_SERVER['HTTP_REFERER'];
                parent::error($message, $jumpUrl);
            }
            exit;
        }
    }

    /**
     * 完整地址
     */
    protected function full_url($url) {
        if (substr($url, 0, 4) == 'http') {
            return $url;
        } else {
            return $this->http . '://' . $_SERVER['HTTP_HOST'] . '/' . ltrim($url, '/');
        }
    }

    protected function make_rand_string() {
        return uniqid() . date(s);
    }


    /**
     * 验证用户状态
     */
    public function checkLogin() {
        $auth = cookie('_auth');
        if (!$auth) {
            redirect(U('login/index'));
        }
        $token = authcode($auth);
        if (!$token) {
            redirect(U('login/index'));
        }
        $user_info = M('Manager')->where(['token' => $token])->find();
        cookie('_auth', $auth);
        $this->admin_info = $user_info;
    }

    /**
     * 验证码用户权限
     */
    public function checkAuth() {
        if (!$this->admin_info) {
            redirect(U('login/index'));
        }
        $group_info = M('ManagerGroup')->find($this->admin_info['group']);
        if (!$group_info) {
            redirect(U('login/index'));
        }
        $this->admin_info['group_name'] = $group_info['name'];
        if ($group_info['supper'] != 1) {  //非超级管理员
            $this->admin_info['supper'] = 0;
            $purview_id = string2array($group_info['purview']);
            if (!$purview_id) {
                $purview_id = [0];
            }
            $this->purviews = M('Purview')->where(['id' => ['in', $purview_id]])->select(); //全部权限
            $in_purview = [];
            foreach ($this->purviews as $purview) {
                $in_purview = array_merge($in_purview, string2array($purview['purview']));
            }

            $NO_AUTHEN = C('NO_AUTHEN');
            if ($NO_AUTHEN) {
                foreach ($NO_AUTHEN as $val) {
                    list($passController, $passAction) = explode('/', $val);
                    if (strtolower($passController) == strtolower(CONTROLLER_NAME) && ($passAction == '*' || strtolower($passAction) == strtolower(ACTION_NAME))) {
                        return; //跳过验证
                    }
                }
            }
            $current = CONTROLLER_NAME . '.' . ACTION_NAME;
            if (!in_array($current, $in_purview)) {
                if (IS_AJAX) {
                    $this->error('无操作权限');
                } else {
                    $this->display('_layout/no_purview');
                    exit();
                }
            }
        } else {
            $this->admin_info['supper'] = 1;
        }

        $this->assign('admin_info', $this->admin_info);
    }

    //公用方法
    public function treeCategory(array $arr, $selected = 0) {
        if (!is_array($arr) || empty($arr)) {
            return;
        }

        $Tree = new \Common\Lib\Tree($arr);
        $html = "<option data-type='\$type' value='\$id' \$selected>\$spacer \$name</option>";
        $result = $Tree->get_tree(0, $html, $selected, '&nbsp;&nbsp;&nbsp;&nbsp;');
        return $result;
    }
}
